// 实现这个项目的构建任务
const { src, dest, series, parallel, watch } = require('gulp')

const del = require('del')
const browserSync = require('browser-sync')

const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
// 创建一个开发服务器
const bs = browserSync.create()
// 清除任务
const clean = () => {
  return del(['dist', 'temp'])
}
// 样式编译任务
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({outputStyle: 'expanded'}))
    .pipe(dest('temp'))
    .pipe(bs.reload({stream: true}))
}
// 脚本编译任务
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({presets: ['@babel/preset-env']}))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}
// 页面模板编译任务
const page = () => {
  return src('src/*.html', {base: 'src'})
    .pipe(plugins.swig({ data }))
    .pipe(dest('temp'))
    .pipe(bs.reload({stream: true}))
}
// 图片编译任务
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}
// 字体编译任务
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}
// 其它编译任务
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}
// 启动调试服务器任务
const serve = () => {
  // 监视源代码文件，如果有改变重新执行相应的编译任务
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/script/*.js', script)
  watch('src/*.html', page)

  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload)

  bs.init({
    notify: false, // 关闭提示
    port: 8888, // 端口
    server: {
      baseDir: ['temp', 'src', 'public'], // 文件查找目录
      // 开发环境下将引用中的/node_modules 映射到 项目中的node_modules
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}
// 压缩任务
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyJS: true,
      minifyCSS: true
    })))
    .pipe(dest('dist'))
}

// 编译 src 文件夹下的内容
const compile = parallel(style, script, page)
// 编译所有内容 即上线前应该执行这个任务
// 编译前应该先走清除任务
const build = series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  )
)
// 启动开发服务器，服务器启动器前应该先编译一次
const start = series(compile, serve)
// 导出任务
module.exports = {
  clean,
  build,
  start,
  serve
}